define(['jquery','bootstrap', 'backend', 'table', 'form','upyun','select2','summernote'], function ($, undefined, Backend, Table, Form,UpYun,Select2,Summernote) {
		var Controller = {
				index: function () {
						var table = $("#table");
						$(document).on('click', '.thisaddtabs', function (e) {
								Backend.api.addtabs(Backend.api.replaceids(this, $(this).attr('url')), $(this).attr("title"));
								return false;
						});
						$(document).on('click','.btn-import',function(e){
								require(['upload'], function (Upload) {
										Upload.api.plupload($(".btn-import", "#toolbar"), function (data, ret) {
												$.ajax({
														type: "post",
														url: "/cms.php/area.area/import.html",
														data: {file: data.url},
														dataType: 'JSON',
														success: function (datak) {
																location.reload();
														},
														error: function () {
																alert('编辑失败');
														},
														complete: function () {
														}
												});
										});
								});
						});
						$(document).on('click', '#chkSelectAll', function (e) {
								var c = $(this).attr("checked");
								if(typeof(c)=='undefined'){
										$(this).attr("checked","");
								}else{
										$(this).removeAttr("checked");
								}
								$("#table input[name=btSelectItem]").each(function(){
										$(this).prop("checked",!$(this).prop("checked"));
								});
								if($("#table input[name=btSelectItem]:checked").length>0){
										$(".btn-del").removeClass('disabled');
								}else{
										$(".btn-del").addClass('disabled');
								}
								return false;
						});
						$(document).on('click', 'input[name=btSelectItem]',function (e) {
							var ll = $("input[name=btSelectItem]:checked").length;
			                if(ll > 0){
			                    if(ll == 1){
			                        $(".btn-edit").removeClass('disabled');
			                    }else{
			                        $(".btn-edit").addClass('disabled');
			                    }
			                    $(".btn-del").removeClass('disabled');
			                    if(typeof($(".btn-more"))!='undefined'){
			                        $(".btn-more").removeClass('disabled');
			                    }
			                }else{
			                    $(".btn-edit").addClass('disabled');
			                    $(".btn-del").addClass('disabled');
			                    if(typeof($(".btn-more"))!='undefined'){
			                        $(".btn-more").addClass('disabled');
			                    }
		                	}
						});
						$(document).on('click','.cheng_showlist input[type=checkbox]',function(e){
								$.ajax({
										type: "post",
										url: "/cms.php/area.area/showlist.html",
										data: {ids:$(this).val(),ischeck:$(this).prop('checked')},
										dataType: 'JSON',
										success: function (data, status) {
										},
										error: function () {
												alert('编辑失败');
										},
										complete: function () {}
								});
						});
						$(".btn-edit").on("click",function(e){
							e.preventDefault();
							var chenked = $("input[name='btSelectItem']:checked");
							Backend.api.addtabs(Backend.api.replaceids(this, 'area.area/edit?ids='+chenked[0].value),'编辑');
							return false;
						});
						$(document).on("click",".btn-del",function (e) {
							e.preventDefault();
							var chenked = $("input[name='btSelectItem']:checked");
							var ids = "";
							for(var i=0;i<chenked.length;i++){
									ids += chenked[i].value+",";
							}
							if(chenked.length==0){
									var index2 = Layer.confirm('请选择记录','请选择');
							}else{
									var id = $(this).data("id");
									var that = this;
									var index = Layer.confirm(
											__('Are you sure you want to delete this item?'),
											{icon: 3, title: __('Warning'), shadeClose: true},
											function () {
													$.ajax({
															type: "post",
															url: "/cms.php/area.area/dels.html",
															data: {ids:ids},
															dataType: 'JSON',
															success: function (data, status) {
																	if (status == "success") {
																			Layer.close(index);
																			location.reload();
																	}
															},
															error: function () {
																			alert('编辑失败');
															},
															complete: function () {}
													});
											}
									);
							}
						});
						$(document).on("click",".btn-delone",function(e){
							var ids = $(this).prop("title");
							var index = Layer.confirm(
								__('Are you sure you want to delete this item?'),
								{icon: 3, title: __('Warning'), shadeClose: true},
								function () {
										$.ajax({
												type: "post",
												url: "/cms.php/area.area/del.html",
												data: {ids:ids},
												dataType: 'JSON',
												success: function (data, status) {
														if (status == "success") {
																Layer.close(index);location.reload();
														}
												},
												error: function () {
														alert('编辑失败');
												},
												complete: function () {}
										});
								}
							);
						});
						$("#reset").on('click',function(e){
					
							$("#s-name").val('');
							$("#s-name").attr('value','');
							// $("input[name='search[count]']").each(function(i){
							// 	$(this).removeAttr('checked');
							// });
						});
						$('div.keep-open ul li[role="menuitem"] input[type="checkbox"]').on('change', function(e){
								var cn = $(this).attr("data-field");
								var cnc = Fast.api.ucfirst(cn);
								if($(this).is(':checked')){
											if($("#table thead").is(":hidden")){
													addColumnMobile(cn);
											}else{
													addColumnPc(cn);
											}
								}else{
											if($("#table thead").is(":hidden")){
													$("#datalist .card-view[columnname="+cn+"]").remove();
													$("#table thead tr th[data-field='"+cn+"']").remove();
											}else{
													//隐藏那个列
													$("#datalist tr td[columnname="+cnc+"]").remove();
													$("#table thead tr th[data-field='"+cn+"']").remove();
											}
								}
						});
						//表格切换
						$("#toggleview").on('click', function (e) {
							if($("#table thead").is(":hidden")){
									//转换table视图
									$("#table thead").show();
									var ss = '';
									$("#datalist tr").each(function(i){
											var obj = $(this);
											ss += '<tr data-index="'+i+'">';
											$.each(eval(pagedata.columns),function(k,v){
													if(v.isshowlist==1){
															switch(k.toLowerCase()){
																	case 'itemid':
																			ss += '<td class="bs-checkbox" columnname="'+v.columnenname+'">';
																			ss += '<input data-index="'+i+'" name="btSelectItem" type="checkbox" value="'+obj.find("div.card-view [columnname='"+k.toLowerCase()+"'] input[type='checkbox']").val()+'" />';
																	break;
																	default:
																			var s4 = obj.find("div.card-view[columnname='"+v.columnenname+"'] span.value").html();
																					if(typeof(s4)!='undefined'){
																							ss += '<td columnname="'+k+'">';
																							ss += s4;
																					}
																			}
																	ss += '</td>';
															}
													});
													ss += '<td columnname="caozuo">';
															ss += obj.find("div.card-view[columnname='caozuo'] span.value").html();
													ss += '</td>';
											ss += '</tr>';
									});
									$("#datalist").html(ss);
							}else{
								//转换cardview视图
								$("#table thead").hide();
								var ss = '';
								$("#datalist tr").each(function(i){
									var obj = $(this);
									ss += '<tr data-index="0">';
									ss += '<td>';
									ss += '<div class="card-views">';
									$.each(eval(pagedata.columns),function(k,v){
											if(v.isshowlist==1){
													ss += '<div class="card-view" columnname="'+v.columnenname+'">';
													switch(k.toLowerCase()){
															case 'itemid':
																	ss += '<input data-index="'+i+'" name="btSelectItem" type="checkbox" value="'+obj.find("td[columnname='"+k.toLowerCase()+"'] input[type='checkbox']").val()+'" />';
															break;
															default:
																	var s4 = obj.find("td[columnname='"+k+"']").html();
																	if(typeof(s4)!='undefined'){
																			ss += '<span class="title">'+v.columnname+'</span><span class="value">'+s4+'</span>';
																	}
													}
													ss += '</div>';
											}
									});
									ss += '<div class="card-view" columnname="caozuo">';
											ss += '<span class="title" style="text-align: center; vertical-align: middle; ">操作</span>';
											ss += '<span class="value">'+obj.find("td[columnname='caozuo']").html()+'</span>';
									ss += '</div>';
									ss += '</div>';
									ss += '</td>';
									ss += '</tr>';
								});
								$("#datalist").html(ss);
							}
						});
								var form = $("form.form-commonsearch");
								if ($(".selects3", form).size() > 0) {
										$(".selects3").bind('click',function(){
										var s1 = '';var p = '';
										$.ajax({
												url: '/cms.php/ajax/readcategory?catid='+$(this).attr("data-pid"),
												type: 'post',
												dataType: 'json',
												async: false,
												success: function (ret) {
														$.each(ret.pss,function(k,v){});
														$.each(ret.ps,function(k,v){});
														$.each(ret.cs,function(k,v){
																s1 = s1 + '<li><button class="btn btn-primary currents" id="'+v.id+'"	type="button">'+v.name+'</button>';
																if(v.child>0){
																		s1 = s1 + '&nbsp;<button class="btn btn-primary childs" id="'+v.id+'" type="button">&gt;</button></li>';
																}
														});
												}
										});
										var s = '<div class="container">';
										s = s + '<div class="row">';
												s = s + '<div class="col-xs-12">';
														s = s + '<ul id="breadcrumb" class="breadcrumb">';
																s = s + '<li><a href="javascript:;" title="' + $(this).attr("data-pid") + '">回到顶级</a> <span class="divider">></span></li>';
														s = s + '</ul>';
												s = s + '</div>';
										s = s + '</div>';
										s = s + '<div class="row">';
												s = s + '<ul id="dcategory" class="dcategory">';
														s = s + s1;
												s = s + '</ul>';
										s = s + '</div>';
										s = s + '</div>';
										$(document).delegate(".breadcrumb a","click",function(){
										var s = '';var p = '';
										$.ajax({
												url: '/cms.php/ajax/readcategory?catid='+$(this).prop("title"),
												type: 'post',
												dataType: 'json',
												success: function (ret) {
														$.each(ret.pss,function(k,v){
																p = p + '<li><a href="javascript:;" title="' + v.id + '">';
																p = p + v.name + '</a>';
																p = p + '<span class="divider">></span></li>';
														});
														$.each(ret.ps,function(k,v){
																p = p + '<li><a href="javascript:;" title="' + v.id + '">';
																p = p + v.name + '</a>';
																p = p + '<span class="divider">></span></li>';
														});
														$.each(ret.cs,function(k,v){
																	s = s + '<li><button class="btn btn-primary currents" id="'+v.id+'"	type="button">'+v.name+'</button>';
																	if(v.child>0){
																			s = s + '&nbsp;<button class="btn btn-primary childs" id="'+v.id+'" type="button">&gt;</button></li>';
																	}
														});
														$("#breadcrumb").html('<li><a href="javascript:;" title="0">回到顶级</a> <span class="divider">></span></li>'+p);
														$("#dcategory").html(s);
												}
										});
										});
										$(document).delegate("button.childs","click",function(){
														var s = '';var p = '';
														$.ajax({
																url: '/cms.php/ajax/readcategory?catid='+$(this).prop("id"),
																type: 'post',
																dataType: 'json',
																success: function (ret) {
																		$.each(ret.pss,function(k,v){
																				p = p + '<li><a href="javascript:;" title="' + v.id + '">';
																				p = p + v.name + '</a>';
																				p = p + '<span class="divider">></span></li>';
																		});
																		$.each(ret.ps,function(k,v){
																				p = p + '<li><a href="javascript:;" title="' + v.id + '">';
																				p = p + v.name + '</a>';
																				p = p + '<span class="divider">></span></li>';
																		});
																		$.each(ret.cs,function(k,v){
																				s = s + '<li><button class="btn btn-primary currents" id="'+v.id+'"	type="button">'+v.name+'</button>';
																				if(v.child>0){
																						s = s + '&nbsp;<button class="btn btn-primary childs" id="'+v.id+'" type="button">&gt;</button></li>';
																				}
																		});
																		$("#breadcrumb").html('<li><a href="javascript:;" title="0">回到顶级</a> <span class="divider">></span></li>'+p);
																		$("#dcategory").html(s);
																}
														});
												});
												$(document).delegate("button.currents","click",function(){
														$(".selected3").html($(this).html());
														$("#catid").val($(this).prop("id"));
														layer.closeAll();
												});
												var ll = layer.open({
														type: 1
														,title: '选择类别'
														,content: s
														,anim: 'down'
														,closeBtn: true
														,area:['90%','90%']
														,style: 'position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
												});
									});
						}
										if($(window).width()<768){
										$("#toggleview").trigger('click');
										}
										$(window).resize(function(){
										if($(window).width()<=768){
										if(!$("#table thead").is(":hidden")){
												$("#toggleview").trigger('click');
										}
										}
										if($(window).width()>768){
										if($("#table thead").is(":hidden")){
												$("#toggleview").trigger('click');
										}
										}
										});
								Controller.api.bindevent();
						},
						add: function () {
								$(document).on("change","#c-provinceid",function(e){
									var s = $(this).data("relate");
									var currentid = '';
									$.ajax({
										type: "post",
										url: "/api/index/city.html",
										data: {pid:$(this).val()},
										dataType: 'JSON',
										success: function (ret) {
											var str = '';
											str += '<option value="0">请选择</option>';
											$.each(eval(ret), function (k, v) {
												str += '<option value="' + v.itemid + '"';
												if(v.itemid==currentid||v.selected==1){
												    str += ' selected="selected">';
												}else{
												    str += '>';
												}
												str += v.name;
												str += '</option>';
											});
											  /*
											  $.each(eval(ret), function (k1, v1) {
											      $.each(eval(v1), function (k, v) {
											          str += '<option value="' + v.itemid + '"';
											          if(v.selected==1){
											              str += ' selected="selected">';
											          }else{
											              str += '>';
											          }
											          str += v.name;
											          str += '</option>';
											      });
											  });
											  */
											  $("#"+s).html(str);
											  $("#"+s).selectpicker('refresh');
										},
										error: function () {alert('编辑失败');},
										complete: function () {}
									});
								});
								Controller.api.bindevent();
						},
						edit: function () {
								$(document).on("change","#c-provinceid",function(e){
									var s = $(this).data("relate");
									var citys = $("#"+s).val();
									var currentid = '';
									$.ajax({
										type: "post",
										url: "/api/index/city.html",
										data: {pid:$(this).val(),cityids:citys},
										dataType: 'JSON',
										success: function (ret) {
											var str = '';
											str += '<option value="0">请选择</option>';
											$.each(eval(ret), function (k, v) {
		                                        str += '<option value="' + v.itemid + '"';
		                                        if(v.itemid==currentid||v.selected==1){
		                                            str += ' selected="selected">';
		                                        }else{
		                                            str += '>';
		                                        }
		                                        str += v.name;
		                                        str += '</option>';
		                                    });
											/*
											$.each(eval(ret), function (k1, v1) {
											  $.each(eval(v1), function (k, v) {
											      str += '<option value="' + v.itemid + '"';
											      if(v.selected==1){
											          str += ' selected="selected">';
											      }else{
											          str += '>';
											      }
											      str += v.name;
											      str += '</option>';
											  });
											});
											*/
											$("#"+s).html(str);
											$("#"+s).selectpicker('refresh');
										},
										error: function () {alert('编辑失败');},
										complete: function () {}
									});
								});
								$(".returnback").click(function(){
										history.back();
								});
								Controller.api.bindevent();
						},
						api: {
								bindevent: function () {
										Form.api.bindevent($("form[role=form]"));
								}
						}
				};
				return Controller;
		});
function creatList(obj, list){
		$.each(eval(obj), function(i,item) {
				var temp = new Object();
				temp.text = item.name;
				temp.value = item.id;
				list.push(temp);
		});
}
//补足表格的列(PC)
function addColumnPc(key){
		var l = 10000;//初始化顺序
		var columnenname = '';var columnname = '';
		$.each(eval(pagedata.columns),function(k,v){
				var temp = new Object();
				if(k.toLowerCase()==key){
						columnenname = v.columnenname;
						columnname = v.columnname;
						l = v.listorder;
				}
				if((v.listorder>l)&&(v.isshowlist==1)){
						var html = "";
						$("#table thead").find("tr th[data-field='"+v.columnenname+"']").before('<th data-field="'+columnenname+'"><div class="th-inner">'+columnname+'</div><div class="fht-cell"></div></th>');
						var list = eval(pagedata.list);
						var cv = '';
						var lst = $("#table tbody tr").find("td[columnname="+Fast.api.ucfirst(v.columnenname)+"]");
						//把单元格的列加上
						lst.each(function(i){
								$(this).before('<td columnname="'+Fast.api.ucfirst(columnenname)+'">'+list[i][columnenname.toLowerCase()]+'</td>');
						});
						return false;
				}
		});
}
//补足表格的列(Mobile)
function addColumnMobile(key){
		var l = 10000;//初始化顺序
		var columnenname = '';var columnname = '';
		$.each(eval(pagedata.columns),function(k,v){
				var temp = new Object();
				if(k.toLowerCase()==key){
						columnenname = v.columnenname;
						columnname = v.columnname;
						l = v.listorder;
				}
				if((v.listorder>l)&&(v.isshowlist==1)){
						var html = "";
						var list = eval(pagedata.list);
						var cv = '';
						var lst = $("#datalist tr td").find("div[columnname="+v.columnenname+"]");
						//把单元格的列加上
						lst.each(function(i){
								$(this).before('<div class="card-view" columnname="'+columnenname.toLowerCase()+'"><span class="title">'+columnname+'</span><span class="value">'+list[i][columnenname.toLowerCase()]+'</span></div>');
						});return false;
				}
		});
		return false;
}
